// const express = require('express');
// const app = express();
// const bodyParser = require('body-parser');
// const mysql = require('mysql');
// const http = require('http');
// const WebSocket = require('ws');
// const wss = new WebSocket.Server({
//     port: 1001
// });


// wss.on('connection', ws =>{
//     console.log('客户端已连接');
//     ws.on('close', () =>{
//         console.log('客户端已断开');
//     })

//     ws.on('message', message => {
//         ws.send(message)
//         console.log('收到客户端消息：', message)
//     })
// })

// app.get('/chatting', (req, res) => {
//     res.header('Access-Control-Allow-Origin', '*');
//     console.log('chatting');
//     res.send('chatting')
// })




// app.get('/ZhuoMian', (req, res) => {
//     res.header('Access-Control-Allow-Origin', '*');
//     // 连接数据库
//     const connection = mysql.createConnection({
//         host: 'localhost',
//         user: 'root',
//         password: '123456',
//         database: 'gameguanlipingtai'
//     });
//     connection.connect();
//     // 查询语句
//     const sql = 'SELECT * FROM zhuomianruanjian';
//     connection.query(sql, (err, result) => {
//         if (err) throw err;
//         console.log(result);
//         res.send(result)
//         connection.end();
//     });

// })

// app.listen(1111, () => {
//     console.log('1111端口启动成功');
// })


const http = require('http');
const socketIo = require('socket.io');

const server = http.createServer((req, res) => {
    res.end('Hello World!');
});

const io = socketIo(server, {
    cors: {
        origin: '*',
        methods: ['GET', 'POST'],
        credentials: true
    }
});

io.on('connection', (socket) => {
    console.log('一个用户已连接');

    socket.on('chat message', (msg) => {
        io.emit('chat message', msg);
    }); 

    socket.on('disconnect', () => {
        console.log('用户已断开连接');
    });
});

io.listen(3001);

server.listen(3000, () => {
    console.log('服务器运行在 http://localhost:3000/');
});